<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Form Validator</title>
  </head>
  <body>
    <div class="container">
      <form id="form" class="form">
        <h2>注册</h2>
        <div class="form-control">
          <label for="username">用户名</label>
          <input type="text" id="username" placeholder="请输入用户名" />
          <small>错误提示</small>
        </div>
        <div class="form-control">
          <label for="email">邮箱</label>
          <input type="text" id="email" placeholder="请输入邮箱" />
          <small>错误提示</small>
        </div>
        <div class="form-control">
          <label for="password">密码</label>
          <input type="password" id="password" placeholder="请输入密码" />
          <small>错误提示</small>
        </div>
        <div class="form-control">
          <label for="password2">确认密码</label>
          <input type="password" id="password2" placeholder="请输入确认密码" />
          <small>错误提示</small>
        </div>
        <button>提交</button>
      </form>
    </div>
    <script src="index2.js"></script>
    <!-- <script>
        // 1.先把按钮的默认跳转样式去掉
        // 2.输入信息，点击按钮，循环判断下input的value值，
        // 3.用正则判断，如果错误就给对应form-control添加error类, 并替换small的提示
        // 4.如果正确，则给对应的form-control添加success类，并删除small的提示
        // 5.在判断所有数据都正确后，就返回turn，启用button的默认提交功能   

        // 获取按钮，并禁用默认样式
        let btn = document.querySelector('button');
        let input = document.querySelectorAll('input'); 
        let password = document.querySelector('#password');
        btn.addEventListener("click", function(e) {
            let num = 0;
            e.preventDefault();
            input.forEach(function(item) {
                if (item.value == "") {
                    item.parentNode.classList.add('form-control-error');
                    item.parentNode.lastElementChild.innerHTML = "此项为必填项";
                } else {
                    if (item.id == 'username') {
                        if (item.value.search(/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/) === -1 ) {
                            item.parentNode.classList.add('form-control-error');
                            item.parentNode.lastElementChild.innerHTML = "用户名输入格式错误";
                        } else {
                            item.parentNode.classList.remove('form-control-error');
                            item.parentNode.classList.add('form-control-success');
                            num++;
                        }
                    } else if (item.id == "email") {
                        if (item.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) === -1 ) {
                            item.parentNode.classList.add('form-control-error');
                            item.parentNode.lastElementChild.innerHTML = "邮箱输入格式错误";
                        } else {
                            item.parentNode.classList.remove('form-control-error');
                            item.parentNode.classList.add('form-control-success');
                            num++;
                        }
                    } else if (item.id == "password") {
                        if (item.value.search(/^[a-zA-Z]\w{5,17}$/) === -1 ) {
                            item.parentNode.classList.add('form-control-error');
                            item.parentNode.lastElementChild.innerHTML = "密码输入格式错误";
                        } else {
                            item.parentNode.classList.remove('form-control-error');
                            item.parentNode.classList.add('form-control-success');
                            num++;
                        }
                    } else if (item.id == "password2") {
                        let str = password.value;
                        if (item.value.search(/^[a-zA-Z]\w{5,17}$/) === -1 ) {
                            item.parentNode.classList.add('form-control-error');
                            item.parentNode.lastElementChild.innerHTML = "密码输入格式错误";
                        } else {
                            if (item.value !== str) {
                                item.parentNode.classList.add('form-control-error');
                                item.parentNode.lastElementChild.innerHTML = "输入的密码与设置的不一致";
                            } else {
                                item.parentNode.classList.remove('form-control-error');
                                item.parentNode.classList.add('form-control-success');
                                num++;
                            }
                        }
                    }
                } 
            });
            if (num === input.length) {
                console.log(true);
            } else {
                console.log(false);
            }
        }); -->


    </script> 
  </body>
</html>
